<?php

class Navbar_Model extends CI_Model {
	
	/*
	 * Get hot games with condition
	 * is_hotgame = 1
	 * status = 1
	 * 
	 * Order by ordering
	 */
	function getHotGames(){		
		$this->db->from('games');
		$this->db->where('is_hotgame', 1);
		$this->db->where('status', 1);		
		$this->db->select("*");
		$this->db->order_by("ordering", "asc");
		$this->db->limit(4);
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
	}
    
    /*
	 * Get all games
	 * status = 1
	 * 
	 * Order by ordering
	 */
	function getAllGames(){		
		$this->db->from('games');
		$this->db->where('status', 1);		
		$this->db->select("*");
		$this->db->order_by("ordering", "asc");
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
            $rs = $query->result();
		}
		return $rs;
	}
    
    function getGameDetail($game_id){		
		$this->db->from('games');
        $this->db->where('id', $game_id);	
		$this->db->where('status', 1);		
		$this->db->select("*");
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
            $rs = $query->row();
		}
		return $rs;
	}
    function getGamePlayer($game_id, $player_id)
    {
        $this->db->from('player_games');
        if ($game_id != null)
            $this->db->where('game_id', $game_id);
        $this->db->where('player_id', $player_id);
        $query = $this->db->get();
        $rs = array();
       	if($query->num_rows() > 0) {
       	    if ($game_id != null)
                $rs = $query->row();
            else
                $rs = $query->result();
		}
		return $rs;
    }
    
    function getCount_GamePlayer($game_id, $player_id)
    {
        $this->db->from('player_games');
        if ($game_id != null)
            $this->db->where('game_id', $game_id);
        $this->db->where('player_id', $player_id);
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function getPlayer($player_id)
    {
        $this->db->from('player');
        $this->db->where('id', $player_id);
        $query = $this->db->get();
        $rs = array();
       	if($query->num_rows() > 0) {
            $rs = $query->row();
		}
		return $rs;
    }
    function getGameCoinHistory($game_id, $player_game_id, $startRecord, $numberRecord)
    {
        $this->db->from('gamecoinhistory');
        $this->db->where('player_game_id', $player_game_id);
        $this->db->where('game_id', $game_id);
       	$this->db->order_by("id", "desc");
		//$this->db->limit(10);
        $this->db->limit($numberRecord, $startRecord);
        $query = $this->db->get();
        $rs = array();
       	if($query->num_rows() > 0) {
            $rs = $query->result();
		}
		return $rs;
    }
    function getCount_GameCoinHistory($game_id, $player_game_id){
        $this->db->from('gamecoinhistory');
        $this->db->where('player_game_id', $player_game_id);
        $this->db->where('game_id', $game_id);
       	$this->db->order_by("id", "desc");
		$query = $this->db->get();
        return $query->num_rows();
    }
   
    function getCoinExchange($game_id)
    {
        $this->db->from('coinexchange');
        $this->db->where('game_id', $game_id);
       	$this->db->order_by("point", "asc");
        $query = $this->db->get();
        $rs = array();
       	if($query->num_rows() > 0) {
            $rs = $query->result();
		}
		return $rs;
    }
    
    
}